#include "VSingerUi.h"
#include "SingerUi/AnimationCtrl.h"
#include "SingerUi/CoverData.h"
#include "SingerUi/SingerCover.h"
#include "VPicture.h"
#include "VMain.h"
#include "VSongsUi.h"
#include "VSongsEffect.h"


extern SingerCoversDeque g_CoverDeque;

DEFINE_THIS(VSingerCoverEffect);

VSingerCoverEffect::VSingerCoverEffect(void) :
    _animate(NULL),
    _coverdata(NULL),
    _covx(0),
    _covy(0),
    _covw(0),
    _covh(0)
{
    SET_THIS();
    init();
}

VSingerCoverEffect::~VSingerCoverEffect(void)
{
}

void VSingerCoverEffect::init()
{
    initCoverData();
    initData();
}

bool VSingerCoverEffect::render() const
{
    if (!VRRWidget::render())
    {
        return false;
    }

    if (_animate != NULL)
    {
        _animate->SetOpenGL(_covx, _covy, _covw, _covh);
        _animate->Render();
        VMain::resetViewport();
    }

    return true;
}

void VSingerCoverEffect::animate()
{
    if (_animate != NULL)
    {
        _animate->Update();
    }
}

void VSingerCoverEffect::initData() // 这是一个演示版本中的函数，在发行的时候将会剔除
{
    // TODO: 此处可以读数据库

    initCoverCtrl();
    auto_set_position();
}

void VSingerCoverEffect::initCoverCtrl()
{
    _animate = new CSingerAnimationCtrl;
}

void VSingerCoverEffect::initCoverData()
{
    _coverdata = new CSingerCoverData;
}

void VSingerCoverEffect::push_back(singer_t* singer)
{
    _coverdata->AddCoverBack(singer);
}

// void VSingerCoverEffect::pop_back()
// {
//     _coverdata->RemoveCoverBack();
// }
//
// void VSingerCoverEffect::pop_front()
// {
//     _coverdata->RemoveCoverFront();
// }


void VSingerCoverEffect::mouseClick(int x, int y)
{
    int i, id = -1;
    singer_t* singer;

    for (i = 0; i < (int)g_CoverDeque.size(); i++)
    {
        singer = g_CoverDeque[i]->Click(x, y);

        if (singer != NULL)
        {
            switch_to_songsuui(singer);
            return;
        }
    }
}

void VSingerCoverEffect::setCoverFlowGeometry(int x, int y, unsigned int w, unsigned int h)
{
    setGeometry(x, y, w, h);
    setResponseGeometry(x, y, w, h);

    _covx = x;
    _covy = y;
    _covw = w;
    _covh = h;
}

void VSingerCoverEffect::scroll_left()
{
    _animate->StartAnimation(SCROLL_LEFT);
    ((VSingerUi*)parent())->autoSet();
}

void VSingerCoverEffect::scroll_right()
{
    _animate->StartAnimation(SCROLL_RIGHT);
    ((VSingerUi*)parent())->autoSet();
}

void VSingerCoverEffect::mouseUp(int x, int y)
{
    if (_pressdown)
    {
        if (_oldx == -1)
        {
            return;
        }


        VSingerUi* ui = (VSingerUi*)VMain::queryUi(UI_SINGER);
        VSingerNavigator* nav = ui->getNav();

        if (x < _oldx)
        {
            scroll_left();
        }
        else if (x > _oldx)
        {
            scroll_right();
        }
    }

    _pressdown = false;
    _oldx = -1;
}

void VSingerCoverEffect::mouseDown(int x, int y)
{
    _oldx = x;
    _pressdown = true;
}

void VSingerCoverEffect::move(int x, int y)
{
    VWidget::move(x, y);

    _covx = x;
    _covy = y;
}

void VSingerCoverEffect::resize(int w, int h)
{
    VWidget::resize(w, h);

    _covw = w;
    _covh = h;
}

void VSingerCoverEffect::clear()
{
    _coverdata->FlushDeque();
}

void VSingerCoverEffect::auto_set_position()
{
    _coverdata->autoSetPosition();
    _animate->SetCenterIndex(g_CoverDeque.size() / 2);
}

int VSingerCoverEffect::current_position() const
{
    return _animate->GetCenterIndx();
}


int VSingerCoverEffect::size() const
{
    return g_CoverDeque.size();
}

void VSingerCoverEffect::scroll_to(int mid)
{
    _animate->SetCenterIndex(mid);
    ((VSingerUi*)parent())->autoSet();
    //  this->scrollRight();
}


void VSingerCoverEffect::animate_left()
{
    return;
}

void VSingerCoverEffect::animate_right()
{
    return;
}

void VSingerCoverEffect::set_item_texture(int itemno, GLuint* avatar, GLuint* name)
{
    return;
}

void VSingerCoverEffect::switch_to_songsuui(singer_t* singer)
{
    ind_singer_song_t* ind = findInd_singer_song(singer);
    int i;
    VSongsUi* ui = (VSongsUi*)VMain::queryUi(UI_SONGS);

    if (ind == NULL)
    {
        // this singer have no any songs
        return;
    }

    if (ind->songs_count == 0)
    {
        return;
    }


    ui->clear();

    for (i = 0; i != ind->songs_count; ++i)
    {
        ui->push_back(ind->songs[i]);
    }

    ui->getEffect()->auto_set_position();
    VMain::switchToUi(ui);
}
